package com.uyun.rbac.permission.web;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uyun.rbac.permission.entity.MenuEntity;
import com.uyun.rbac.permission.entity.PermissionEntity;
import com.uyun.rbac.permission.entity.RoleEntity;
import com.uyun.rbac.permission.service.PermissionService;
import com.uyun.util.AppUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class PermissionController  extends MultiActionController {
 
	@Autowired
	private PermissionService permissionService;



    /**************************************权限部分************************************************/
	@RequestMapping( value="/permission/index")
	public ModelAndView index() {
 		
		return new ModelAndView("permission/index");
 
	}

    @RequestMapping( value="/permission/getList",method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity<List<PermissionEntity>> getList(@RequestParam(required = false) String permissionName,
                                                   @RequestParam(required = false) String mapping,
                                                   @RequestParam(required = false) String url) throws IOException{


        Map<String,String> paraMap = new HashMap<>();
        paraMap.put("permissionName",permissionName);
        paraMap.put("mapping",mapping);
        paraMap.put("url",url);
        List<PermissionEntity> list = permissionService.getPermissionListForPage(paraMap);

        return new ResponseEntity<>(list, HttpStatus.OK);

    }

    @RequestMapping( value="/permission/add",method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity<String> add(@RequestParam String permissionName,
                               @RequestParam String mapping,
                               @RequestParam String url,
                               @RequestParam String type) throws IOException{

        String code = "0";
        PermissionEntity entity = new PermissionEntity();
        entity.setName(permissionName);
        entity.setMapping(mapping);
        entity.setUrl(url);
        try{
            entity.setType(Integer.parseInt(type));
        }catch (Exception ignored){}

        boolean flag = permissionService.addPermissionEntity(entity);

        if(flag)code="1";

        return new ResponseEntity<>(code, HttpStatus.OK);

    }


    /**************************************角色部分************************************************/
    @RequestMapping( value="/permission/role")
    public ModelAndView role() {
        return new ModelAndView("permission/role");

    }

    @RequestMapping( value="/permission/getRoleList",method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity<List<RoleEntity>> getList(@RequestParam String roleName) throws IOException{


        Map<String,String> paraMap = new HashMap<>();
        paraMap.put("roleName",roleName);
        List<RoleEntity> list = permissionService.getRoleListForPage(paraMap);

        return new ResponseEntity<>(list, HttpStatus.OK);

    }

    @RequestMapping( value="/permission/getRoleEntity",method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity<RoleEntity> getRoleEntity(@RequestParam String id) throws IOException{

        int roleId = 0;

        if (AppUtils.StringUtil(id)!=null){
            try{
                roleId = Integer.parseInt(id);
            }catch (Exception ignored){}
        }

        RoleEntity roleEntity = permissionService.getRoleEntityById(roleId);

        return new ResponseEntity<>(roleEntity, HttpStatus.OK);

    }

    @RequestMapping( value="/permission/editRoleHasMenu",method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity<String> editRoleHasMenu(@RequestParam String roleId,
                                           @RequestParam(value="menuIds[]") String[] menuIds){

        String code = "0";

        boolean flag = permissionService.editRoleHasMenu(roleId,menuIds);

        if(flag)code="1";

        return new ResponseEntity<>(code, HttpStatus.OK);

    }

    @RequestMapping( value="/permission/editRoleHasPerm",method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity<String> editRoleHasPerm(@RequestParam String roleId,
                                           @RequestParam(value="permIds[]") String[] permIds){

        String code = "0";

        boolean flag = permissionService.editRoleHasPerms(roleId,permIds);

        if(flag)code="1";

        return new ResponseEntity<>(code, HttpStatus.OK);

    }




    /**************************************菜单部分************************************************/
    @RequestMapping( value="/permission/getMenu",produces="application/json;charset=UTF-8")
	public @ResponseBody
    String getMenu() {

		Subject user = SecurityUtils.getSubject();
        String userName = (String) user.getPrincipal();

        List<MenuEntity> list  = permissionService.getMenuList(userName);

        return JSONObject.parse(JSON.toJSONString(list)).toString();

    }

    @RequestMapping( value="/permission/getAllMenu",produces="application/json;charset=UTF-8")
    public @ResponseBody
    String getAllMenu() {

        List<MenuEntity> list = permissionService.getAllMenu();
        return JSONObject.parse(JSON.toJSONString(list)).toString();

    }






}
